package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.WordsBookActivity;
import cn.edu.zjicm.wordsnet_d.app.WordApp;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.StaticData;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.view.MyTabPageIndicator;
import cn.edu.zjicm.wordsnet_d.view.pageindicator.PageIndicator;

public class WordsBookPagerAdapter extends PagerAdapter {
	private WordsBookActivity activity;

	private ArrayList<OneWord> difficultyWordsList;
	private PageIndicator wordsBookPageIndicator;
	private int mLastItemDifficultyList;

	private RelativeLayout indicatorContainer;
	private MyTabPageIndicator learnWordsPageIndicator;
	private ViewPager learnWordsPager;
	public LearnedWordsPagerAdapter mPagerAdapter;
	private WordFactory wf;

	private ListView difficultyWordListView;
	private TextView tip;
	private WordListAdapter difficultyWordAdapter;

	public WordsBookPagerAdapter(WordsBookActivity activity, PageIndicator pageIndicator) {
		this.activity = activity;
		wordsBookPageIndicator = pageIndicator;
		wf = WordFactory.getInstance(activity);
		difficultyWordsList = new ArrayList<OneWord>();
		difficultyWordsList.addAll(wf.getDifficultyWordsLimit(0, StaticData.BufferSize));
		initLearnedWordsViewPager();
	}

	@Override
	public int getCount() {
		return 2;
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == obj;
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				int index = (Integer) msg.obj;
				learnWordsPager.setCurrentItem(index);
				activity.setCurrentViewPagerIndex(index);
			} else if (msg.what == 2) {

				wordsBookPageIndicator.setCurrentItem(1);
			}
		}
	};

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		LayoutInflater mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		switch (position) {
		case 0: {
			View learnedWordView = mInflater.inflate(R.layout.view_wordsbook_viewpager, null);
			learnWordsPager = (ViewPager) learnedWordView.findViewById(R.id.word_unit_pager);
			indicatorContainer = (RelativeLayout) learnedWordView.findViewById(R.id.word_unit_pager_indicator);
			learnWordsPageIndicator = new MyTabPageIndicator(activity, null);
			mPagerAdapter = new LearnedWordsPagerAdapter(activity, null);
			indicatorContainer.removeAllViews();
			for (int i = 0; i < mPagerAdapter.getCount(); i++) {
				learnWordsPageIndicator.addTab(i, mPagerAdapter.getPageTitle(i));
			}
			learnWordsPageIndicator.setCurrentItem(0);
			learnWordsPageIndicator.setHandler(handler);
			indicatorContainer.addView(learnWordsPageIndicator);
			learnWordsPager.setAdapter(mPagerAdapter);
			learnWordsPager.setOffscreenPageLimit(3);
			learnWordsPager.setOnPageChangeListener(new OnPageChangeListener() {

				@Override
				public void onPageSelected(int arg0) {
					// TODO Auto-generated method stub
					learnWordsPageIndicator.setCurrentItem(arg0);
					activity.setCurrentViewPagerIndex(arg0);
				}

				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onPageScrollStateChanged(int arg0) {
					// TODO Auto-generated method stub

				}
			});
			WordApp.getInstance().setOnWordChangeListener(mPagerAdapter);
			container.addView(learnedWordView);
			return learnedWordView;
		}
		case 1: {
			View difficultyWordView = mInflater.inflate(R.layout.view_date_unit_list, null);
			difficultyWordListView = (ListView) difficultyWordView.findViewById(R.id.word_list1);
			tip=(TextView)difficultyWordView.findViewById(R.id.difficulty_tip);
			if(difficultyWordsList.size()==0)
			{
				tip.setVisibility(View.VISIBLE);
				difficultyWordListView.setVisibility(View.GONE);
			}
			else
			{
				tip.setVisibility(View.GONE);
				difficultyWordListView.setVisibility(View.VISIBLE);
			}
			difficultyWordAdapter = new WordListAdapter(activity, difficultyWordsList, true);
			difficultyWordListView.setOnScrollListener(new OnScrollListener() {
				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					if (mLastItemDifficultyList == difficultyWordAdapter.getCount()) {
						if (loadMoreDifficultyWords()) {
							difficultyWordAdapter.notifyDataSetChanged();
						}
					}
				}

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
					mLastItemDifficultyList = firstVisibleItem + visibleItemCount;
				}
			});
			difficultyWordListView.setAdapter(difficultyWordAdapter);
			container.addView(difficultyWordView);
			if (activity.isOnDifficulty == 1) {
				Message msg = new Message();
				msg.what = 2;
				handler.sendMessage(msg);
				activity.isOnDifficulty = 0;
			}
			return difficultyWordView;
		}
		}
		return null;
	}

	private void initLearnedWordsViewPager() {

		wordsBookPageIndicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 0) {
					arg0 = learnWordsPager.getCurrentItem();
				} else if (arg0 > 0) {
					arg0 += 2;
				}
				activity.setCurrentViewPagerIndex(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	private boolean loadMoreDifficultyWords() {
		int listSize = difficultyWordsList.size();
		List<OneWord> loadedList = wf.getDifficultyWordsLimit(listSize, StaticData.BufferSize);
		if (null == loadedList)
			return false;
		difficultyWordsList.addAll(loadedList);
		return true;
	}

	public void resetListForEdit() {
		difficultyWordAdapter.notifyDataSetChanged();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public CharSequence getPageTitle(int position) {
		switch (position) {
		case 0:
			return activity.getString(R.string.learned_words_list);
		case 1:
			return activity.getString(R.string.diffcult_words_list);
		}
		return super.getPageTitle(position);
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	public void reLoadList() {
		difficultyWordsList.clear();
		difficultyWordsList.addAll(wf.getDifficultyWordsLimit(0, StaticData.BufferSize));
		if(difficultyWordsList.size()==0)
		{
			tip.setVisibility(View.VISIBLE);
			difficultyWordListView.setVisibility(View.GONE);
		}
		else
		{
			tip.setVisibility(View.GONE);
			difficultyWordListView.setVisibility(View.VISIBLE);
		}
		resetListForEdit();
	}

	public void allSelect() {
		activity.wordList.clear();
		activity.wordList.addAll(difficultyWordsList);
	}

	public boolean isAllSelect() {
		return activity.wordList.size() == difficultyWordsList.size();
	}

}